package com.ielts.services;

import java.io.IOException;

import org.apache.http.Header;
import org.json.JSONArray;

import vn.com.baseapplication.BaseApplication;
import vn.com.baseapplication.constant.BaseConstant;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.ielts.IeltsConstant;
import com.ielts.activity.IeltsApplication;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class GcmBackground extends AsyncTask<String, String, String> {
	BaseApplication application;
	String msg = "";
	GoogleCloudMessaging gcm;

	String host = "http://192.168.0.100";
	String subHost = "/reader_book/rest";
	String gcmId = "";
	Handler handler;

	int loadMaxCnt = 2;
	public static int loadCnt = 0;

	public GcmBackground(BaseApplication application) {
		this.application = application;
		handler = new Handler();
		loadCnt++;
	}

	public static String getGcmId(Context context) {
		GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
		try {
			return gcm.register(IeltsConstant.SENDER_ID);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "-1";
	}

	@Override
	protected String doInBackground(String... params) {
		try {
			if (gcm == null) {
				gcm = GoogleCloudMessaging.getInstance(application);
			}
			gcmId = gcm.register(IeltsConstant.SENDER_ID);
			msg = "Dvice registered, registration ID=" + gcmId;

			// sendIdToServerTest("ielts", "pass", "123", gcmId,
			// "ielts@gmail.com", "avatar", "user", "description", "ielts");
		} catch (IOException ex) {
			Log.e("GCM ERR", ex.toString());
			msg = null;
		}
		return msg;
	}

	@Override
	protected void onPostExecute(String result) {
		super.onPostExecute(result);
		Log.e("GCM", result + "");
		if (loadCnt < loadMaxCnt) {
			if (gcmId == null || gcmId.equals("")) {
				new GcmBackground(application).execute();
				Toast.makeText(application, "Retrying to get ID", Toast.LENGTH_SHORT).show();
				return;
			}
		}
		IeltsApplication.getInstance().setValue(IeltsConstant.KEY_GCM_ID, gcmId);
		Intent intent = new Intent();
		intent.putExtra(BaseConstant.MSG_FLAG, IeltsConstant.LOAD_DONE+"");
		this.application.sendBaseBroadcast(application, intent);
	}

	public void sendIdToServerTest(final String userName, final String password, final String deviceId, final String gcmId, final String email, final String avatar, final String permission, final String description, final String appName) {
		String url = host + subHost + "/user/reg_gcm.php";

		application.setValue(IeltsConstant.KEY_GCM_ID, gcmId);

		AsyncHttpClient http = new AsyncHttpClient();

		RequestParams param = new RequestParams();
		param.add("user_name", userName);
		param.add("password", password);
		param.add("device_id", deviceId);
		param.add("gcm_id", gcmId);
		param.add("email", email);
		param.add("avatar", avatar);
		param.add("permission", permission);
		param.add("description", description);
		param.add("app_name", appName);

		http.post(application, url, param, new JsonHttpResponseHandler() {
			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				Log.e("GCM Success", gcmId);
			}

			@Override
			public void onFailure(Throwable e, JSONArray errorResponse) {
				// TODO Auto-generated method stub
				super.onFailure(e, errorResponse);
				Log.e("GCM Failed", gcmId);
			}
		});
	}
}
